package com.zfj.TreeSet;

import com.zfj.HashSet.Person;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author zfj
 * @date 2022/3/7 -16:29
 * TreeSet集合使用
 * Comparator实现定制比较
 * Comparable 可比较的
 */
public class Demo3 {
	public static void main(String[] args) {
		TreeSet<Person> people=new TreeSet<>(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				int n1=o1.getAge()-o2.getAge();
				int n2=o1.getName().compareTo(o2.getName());
				return n1==0?n1:n2;
			}
		});
		Person p1=new Person("张三",28);
		Person p2=new Person("李四",22);
		Person p3=new Person("王五",25);
		people.add(p1);
		people.add(p2);
		people.add(p3);
		System.out.println(people.toString());
	}
}
